package Arrays101.Day02;

import java.lang.annotation.ElementType;

public class Day02_1 {
    public static void main(String[] args) {

        duplicateZeros(new int[]{1,0,2,3,0,4,5,0});
    }

    /**
     * Duplicate Zeros
     *
     * Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.
     * Note that elements beyond the length of the original array are not written.
     * Do the above modifications to the input array in place, do not return anything from your function.
     * @param arr
     */
    public static void duplicateZeros(int []arr){
        int a=0;
        for (int i = 0; i < arr.length; i++) {

            if (arr[i]==0){
                a++;
            }
        }
        int temp[]=new int[arr.length+a];

        for (int i = 0,j=0; i < arr.length; i++,j++) {
            if (arr[i]==0){
                temp[j]=0;
                temp[j+1]=0;
                ++j;
            }else {
                temp[j]=arr[i];
            }
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i]=temp[i];
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
}
